package cn.tedu.boot02.controller;

import cn.tedu.boot02.entity.Product;
import cn.tedu.boot02.util.DBUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
public class ProductController {

    List<Product> products = new ArrayList<>();
    @RequestMapping("/insert")
    @ResponseBody
    public String insert(Product product){
        System.out.println("product = " + product.toString());
        try(
                Connection connection = DBUtil.getConnection();
                ) {

            String sql = "insert into product values(null,?,?,?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,product.getTitle());
            preparedStatement.setDouble(2,product.getPrice());
            preparedStatement.setInt(3,product.getNum());
            int i = preparedStatement.executeUpdate();
            if (i>0){
                return "添加成功!<a href='/'>返回首页</a>";
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return "添加失败";
    }

    @RequestMapping("/select")
    @ResponseBody
    public String select() {
        try (
                Connection connection = DBUtil.getConnection();
                ){
            String sql = "select id,title,price,num from product";
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                int id = rs.getInt("id");
                String title = rs.getString("title");
                double price = rs.getDouble("price");
                int num = rs.getInt("num");
                Product product = new Product(id, title, price, num);
                products.add(product);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        String html = "<table border='1'>";
        html+="<caption>商品列表</caption>";
        html+="<tr><th>id</th><th>商品标题</th><th>价格</th><th>库存</th><th>操作</th></tr>";
        for (Product product : products){
            html+="<tr>";
            html+="<td>"+product.getId()+"</td>";
            html+="<td>"+product.getTitle()+"</td>";
            html+="<td>"+product.getPrice()+"</td>";
            html+="<td>"+product.getNum()+"</td>";
            html+="<td>"+"<a href='/delete?id="+product.getId()+"'>删除</a>"+"</td>";
            html+="</tr>";

        }
        html+="</table>";
        return html;
    }
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(int id) {
        System.out.println("id="+id);
        try (
                Connection connection = DBUtil.getConnection();
        ){
            String sql = "delete from product where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1,id);
            int i = ps.executeUpdate();
            if (i>0){
                return "删除成功<a href='/select'>商品列表</a>";
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "删除失败<a href='/select'>商品列表</a>";
    }
    @RequestMapping("/updata")
    @ResponseBody
    public String updata(Product product) {
        System.out.println("product="+product);
        try (
                Connection connection = DBUtil.getConnection();
        ){
            String sql = "update product set title=?,price=?,num=? where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,product.getTitle());
            ps.setDouble(2,product.getPrice());
            ps.setInt(3,product.getNum());
            ps.setInt(4,product.getId());
            int i = ps.executeUpdate();
            if (i>0){
                return "修改成功<a href='/select'>商品列表</a>";
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "修改失败";
    }
}

